package environment

import (
    "strings"

    "git.inspur.com/x/common/loggerfactory"
    "git.inspur.com/x/keycloak"
    rbacv1 "k8s.io/api/rbac/v1"
    "k8s.io/client-go/kubernetes"
)

const (
    ICP = "icp"
    K8S = "k8s"
    IOP = "iop"
)

var logger = loggerfactory.GetLogger()

//go:generate mockgen -destination mocks/mock_cluster.go -package client fcs-api/src/environment Interface
type Interface interface {
    CreateUserRBACSubject(name string) error
    //if icp return serviceAcount else return user
    GetRBACSubject(username string) rbacv1.Subject
    //if icp return serviceAcount token else return token
    ExtractToken(authToken string) string
}

func GetCluster(envType string, k8sClient kubernetes.Interface) Interface {
    if envType == ICP {
        return &icpEnv{k8sClient: k8sClient}
    }
    return &k8cEnv{}
}

func GetRBACSubjectLabelName(username string) string {
    if strings.Index(username, "@") > -1 {
        username = strings.Replace(username, "@", "-at-", -1)
    }
    if strings.Index(username, "_") > -1 {
        username = strings.Replace(username, "_", "---", -1)
    }
    if strings.Index(username, "&") > -1 {
        username = strings.Replace(username, "&", "-and-", -1)
    }
    return keycloak.Client.ClaimName(username)
}
